import time
from selenium.webdriver.common.by import By


class CaiWu:
    # 创建登录方法
    def login(self, driver):
        time.sleep(0.4)
        result = driver.title
        # 退出浏览器
        return result

    # 创建注册方法
    def registered(self, username, password, password2, driver):
        driver.find_element(By.XPATH, '/html/body/div/div/div/div[2]/a').click()
        # 获取账号输入框
        driver.find_element(By.ID, 'username').send_keys(username)
        # 获取密码输入框
        driver.find_element(By.ID, 'password').send_keys(password)
        # 获取重复密码框
        driver.find_element(By.ID, 'repassword').send_keys(password2)
        # time.sleep(0.5)
        # 点击登录按钮
        driver.find_element(By.ID, 'login_btn').click()
        # 获取登录结果
        time.sleep(0.4)
        result = driver.title
        return result

    # 添加银行卡
    def add_bankcard(self, bank_name, bank_card_type, card_number, driver):
        # 获取个人中心点击按钮
        driver.find_element(By.XPATH, '//*[@id="leftbaraside"]/div[2]/nav/ul/li[4]/a/i').click()
        # time.sleep(0.2)

        # 获取银行卡管理并点击
        driver.find_element(By.XPATH, '//*[@id="leftbaraside"]/div[2]/nav/ul/li[4]/ul/li[3]/a').click()
        # time.sleep(0.2)

        # 获取添加一行卡按钮
        driver.find_element(By.XPATH, '//*[@id="bankCard_add_modal_btn"]').click()
        # time.sleep(0.2)
        # 获取银行填入框
        driver.find_element(By.XPATH, '//*[@id="cardBank_add_input"]').send_keys(bank_name)
        # time.sleep(0.5)
        # 获取银行卡类别选择
        if bank_card_type == '借记卡':
            driver.find_element(By.XPATH,
                                '//*[@id="bankCardAddModal"]/div/div/div[2]/form/div[2]/div[1]/div/label[1]/span').click()
        else:
            driver.find_element(By.XPATH,
                                '//*[@id="bankCardAddModal"]/div/div/div[2]/form/div[2]/div[1]/div/label[2]/span').click()
        # 获取银行卡号输入框
        driver.find_element(By.XPATH, '//*[@id="cardNum_add_input"]').send_keys(card_number)
        # time.sleep(0.5)
        # 获取保存按钮
        driver.find_element(By.XPATH, '//*[@id="bankCard_save_btn"]').click()
        time.sleep(0.5)
        result = driver.find_element(By.PARTIAL_LINK_TEXT, '银行卡管理').text
        # 关闭浏览器
        return result

    # 购买基金
    def buy_funds(self, licai_type, licai_chanpin, card_password, driver):
        # 点击个人理财按钮
        driver.find_element(By.XPATH, '//*[@id="leftbaraside"]/div[2]/nav/ul/li[2]/a').click()
        if licai_type == '零钱理财':
            driver.find_element(By.XPATH, '//*[@id="leftbaraside"]/div[2]/nav/ul/li[2]/ul/li[1]/a').click()
            if licai_chanpin == '支付宝零钱理财':
                driver.find_element(By.XPATH, '/html/body/div[1]/div/main/div/div/div/div/div[2]/div/table/tbody/tr[1]/td[6]/button').click()
            elif licai_chanpin == '微信零钱理财':
                driver.find_element(By.XPATH, '/html/body/div[1]/div/main/div/div/div/div/div[2]/div/table/tbody/tr[2]/td[6]/button').click()
            elif licai_chanpin == '理财通零钱理财':
                driver.find_element(By.XPATH, '/html/body/div[1]/div/main/div/div/div/div/div[2]/div/table/tbody/tr[3]/td[6]/button').click()
            elif licai_chanpin == '云闪付理财':
                driver.find_element(By.XPATH, '/html/body/div[1]/div/main/div/div/div/div/div[2]/div/table/tbody/tr[4]/td[6]/button').click()
            elif licai_chanpin == '度小满余额盈':
                driver.find_element(By.XPATH, '/html/body/div[1]/div/main/div/div/div/div/div[2]/div/table/tbody/tr[5]/td[6]/button').click()
        elif licai_type == '期限理财':
            driver.find_element(By.XPATH, '//*[@id="leftbaraside"]/div[2]/nav/ul/li[2]/ul/li[2]/a').click()
            if licai_chanpin == '支付宝期限理财':
                driver.find_element(By.XPATH, '/html/body/div[1]/div/main/div/div/div/div/div[2]/div/table/tbody/tr[1]/td[8]/button').click()
            elif licai_chanpin == 'P2P期限理财':
                driver.find_element(By.XPATH, '/html/body/div[1]/div/main/div/div/div/div/div[2]/div/table/tbody/tr[2]/td[8]/button').click()
            elif licai_chanpin == '度小满月月盈':
                driver.find_element(By.XPATH, '/html/body/div[1]/div/main/div/div/div/div/div[2]/div/table/tbody/tr[3]/td[8]/button').click()
            elif licai_chanpin == '度小满定期盈':
                driver.find_element(By.XPATH, '/html/body/div[1]/div/main/div/div/div/div/div[2]/div/table/tbody/tr[4]/td[8]/button').click()
        elif licai_chanpin == '基金理财':
            driver.find_element(By.XPATH, '//*[@id="leftbaraside"]/div[2]/nav/ul/li[2]/ul/li[3]/a').click()
            if licai_chanpin == '广发多元新兴股票':
                driver.find_element(By.XPATH, '/html/body/div[1]/div/main/div/div/div/div/div[2]/div/table/tbody/tr[1]/td[9]/button').click()
            elif licai_chanpin == '长信利发债券':
                driver.find_element(By.XPATH, '/html/body/div[1]/div/main/div/div/div/div/div[2]/div/table/tbody/tr[2]/td[9]/button').click()
            elif licai_chanpin == '贵州茅台基金':
                driver.find_element(By.XPATH, '/html/body/div[1]/div/main/div/div/div/div/div[2]/div/table/tbody/tr[3]/td[9]/button').click()
        else:
            driver.find_element(By.XPATH, '//*[@id="leftbaraside"]/div[2]/nav/ul/li[2]/ul/li[4]/a').click()
            time.sleep(5)
            driver.quit()
        driver.find_element(By.XPATH, '/html/body/div[3]/div[2]/input').send_keys(card_password)
        driver.find_element(By.XPATH, '/html/body/div[3]/div[3]/a[1]').click()
        time.sleep(0.4)
        # 捕获弹窗文本
        result = driver.find_element(By.XPATH,'/html/body/div[3]/div[2]/div/div/div/div/div/div/div/div[3]/div/div').text
        # 退出浏览器
        return result

    # 申请网贷
    def oline_loans(self, money, day, driver):
        # 点击金融工具
        driver.find_element(By.XPATH, '//*[@id="leftbaraside"]/div[2]/nav/ul/li[3]/a').click()
        # 点击安全网贷
        driver.find_element(By.XPATH, '//*[@id="leftbaraside"]/div[2]/nav/ul/li[3]/ul/li[2]/a').click()
        # 传入借贷金额
        driver.find_element(By.XPATH, '//*[@id="amount"]').send_keys(money)
        # 传入借款期限
        driver.find_element(By.XPATH, '//*[@id="term"]').send_keys(day)
        # 点击申请
        driver.find_element(By.XPATH, '//*[@id="submit"]').click()
        time.sleep(0.2)
        driver.find_element(By.XPATH, '/html/body/div[2]/div[2]/div/div/div/div/div/div/div/div[4]/button[1]').click()
        time.sleep(0.4)
        # 捕获弹窗文本
        result = driver.find_element(By.XPATH, '/html/body/div[2]/div[2]/div/div/div/div/div/div/div/div[3]/div/div').text
        return result
